package MEET.src.code;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MeetingDaoimpl implements MeetingDao1 {
    @Override
    public List<Meeting> searchMeetings(String meetingId, String hostName) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Meeting> meetings = new ArrayList<>();

        try {
            conn = UserDao.getConnection();
            StringBuilder sql = new StringBuilder("SELECT * FROM meeting WHERE Status='已通过'");

            // 添加会议号查询条件
            if (meetingId != null && !meetingId.isEmpty()) {
                sql.append(" AND MID LIKE ?");
            }

            // 添加主持人查询条件
            if (hostName != null && !hostName.isEmpty()) {
                sql.append(" AND Name LIKE ?");
            }

            ps = conn.prepareStatement(sql.toString());

            int paramIndex = 1;
            if (meetingId != null && !meetingId.isEmpty()) {
                ps.setString(paramIndex++, "%" + meetingId + "%");
            }
            if (hostName != null && !hostName.isEmpty()) {
                ps.setString(paramIndex++, "%" + hostName + "%");
            }

            rs = ps.executeQuery();

            while (rs.next()) {
                Meeting meeting = new Meeting(
                        rs.getString("MID"),
                        rs.getString("Mname"),
                        rs.getString("Name"),
                        rs.getTimestamp("Mstarttime"),
                        rs.getTimestamp("Mendtime"),
                        rs.getString("Place"),
                        rs.getString("Location"),
                        rs.getString("Room"),
                        rs.getString("Form"),
                        rs.getString("Type"),
                        rs.getString("CID"),
                        rs.getString("ID"),
                        rs.getString("Status")
                );
                meetings.add(meeting);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
           UserDao.close(conn);
        }

        return meetings;
    }
    @Override
    public Meeting getMeetingDetails(String meetingId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Meeting meeting = null;

        try {
            conn = UserDao.getConnection();
            String sql = "SELECT * FROM meeting WHERE MID = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, meetingId);
            rs = ps.executeQuery();

            if (rs.next()) {
                meeting = new Meeting(
                        rs.getString("MID"),
                        rs.getString("Mname"),
                        rs.getString("Name"),
                        rs.getTimestamp("Mstarttime"),
                        rs.getTimestamp("Mendtime"),
                        rs.getString("Place"),
                        rs.getString("Location"),
                        rs.getString("Room"),
                        rs.getString("Form"),
                        rs.getString("Type"),
                        rs.getString("CID"),
                        rs.getString("ID"),
                        rs.getString("Status")

                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            UserDao.close(conn);
        }

        return meeting;
    }
    @Override
    public String joinMeeting(String meetingId, String userId) {
        // 不做任何数据库操作，直接返回成功
        return "参加成功";
    }

    @Override
    public List<Meeting> getHostedMeetings(String userId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Meeting> meetings = new ArrayList<>();

        try {
            conn = UserDao.getConnection();
            String sql = "SELECT * FROM meeting WHERE host = ? OR account = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, userId);
            ps.setString(2, userId);
            rs = ps.executeQuery();

            while (rs.next()) {
                Meeting meeting = new Meeting(
                        rs.getString("MID"),
                        rs.getString("Mname"),
                        rs.getString("Name"),
                        rs.getTimestamp("Mstarttime"),
                        rs.getTimestamp("Mendtime"),
                        rs.getString("Place"),
                        rs.getString("Location"),
                        rs.getString("Room"),
                        rs.getString("Form"),
                        rs.getString("Type"),
                        rs.getString("CID"),
                        rs.getString("ID"),
                        rs.getString("Status")

                );
                meetings.add(meeting);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            UserDao.close(conn);
        }

        return meetings;
    }

    @Override
    public List<Meeting> searchHistoryMeetings(String searchKey, Timestamp startDate, Timestamp endDate) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Meeting> meetings = new ArrayList<>();

        try {
            conn = UserDao.getConnection();
            StringBuilder sql = new StringBuilder(
                    "SELECT MID, Mname, Name, Mstarttime, Mendtime, Place, Location, Room, Status " +
                            "FROM meeting WHERE status = '已通过' ");

            // 添加主持人查询条件
            if (searchKey != null && !searchKey.isEmpty()) {
                sql.append("AND Name LIKE ? ");
            }

            // 添加日期范围条件
            if (startDate != null && endDate != null) {
                sql.append("AND Mstarttime BETWEEN ? AND ? ");
            } else if (startDate != null) {
                sql.append("AND Mstarttime >= ? ");
            } else if (endDate != null) {
                sql.append("AND Mstarttime <= ? ");
            }

            sql.append("ORDER BY Mstarttime DESC");

            ps = conn.prepareStatement(sql.toString());

            int paramIndex = 1;

            // 设置主持人参数
            if (searchKey != null && !searchKey.isEmpty()) {
                ps.setString(paramIndex++, "%" + searchKey + "%");
            }

            // 设置日期参数
            if (startDate != null && endDate != null) {
                ps.setTimestamp(paramIndex++, startDate);
                ps.setTimestamp(paramIndex++, endDate);
            } else if (startDate != null) {
                ps.setTimestamp(paramIndex++, startDate);
            } else if (endDate != null) {
                ps.setTimestamp(paramIndex++, endDate);
            }

            rs = ps.executeQuery();

            while (rs.next()) {
                Meeting meeting = new Meeting();
                meeting.setMID(rs.getString("MID"));
                meeting.setMname(rs.getString("Mname"));
                meeting.setName(rs.getString("Name"));
                meeting.setMstarttime(rs.getTimestamp("Mstarttime"));
                meeting.setMendtime(rs.getTimestamp("Mendtime"));
                meeting.setPlace(rs.getString("Place"));
                meeting.setLocation(rs.getString("Location"));
                meeting.setRoom(rs.getString("Room"));
                meeting.setStatus(rs.getString("Status"));

                meetings.add(meeting);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) rs.close();
                if (ps != null) ps.close();
                UserDao.close(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return meetings;
    }

    private int executeCountQuery(String sql) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            conn = UserDao.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);

            if (rs.next()) {
                return rs.getInt(1);
            }
            return 0;

        } catch (SQLException e) {
            System.err.println("执行调试查询失败: " + sql);
            e.printStackTrace();
            return -1;
        } finally {
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
                UserDao.close(conn);
            } catch (SQLException e) {
                System.err.println("关闭调试查询资源时出错:");
                e.printStackTrace();
            }
        }
    }

}




























